using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;

namespace WTEvection
{
    public partial class Form1 : InfoForm
    {
        object[] oDiff = null;

       

        public Form1()
        {
            InitializeComponent();
        }

        private void detailNavigatorAddItem_Click(object sender, EventArgs e)
        {
            ToolStripButton button = sender as ToolStripButton;
            DetailNavigator currentParent = button.GetCurrentParent() as DetailNavigator;
            InfoBindingSource bindingSource = currentParent.BindingSource as InfoBindingSource;
           
            DataTable dtDetail = null;
            if (bindingSource.Position != -1)
            {
                dtDetail = (bindingSource.CurrencyManager.Current as DataRowView).Row.Table;
            }
            SelEmpfrm frm = new SelEmpfrm(dtDetail);
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                this.ibsWorkTime_EvectionM.CurrencyManager.EndCurrentEdit();
                DataRowView view = null;
                DataRowView dataBoundItem = null;
                foreach (DataGridViewRow row in frm.dgv.Rows)
                {
                    dataBoundItem = row.DataBoundItem as DataRowView;
                    if (Convert.ToBoolean(dataBoundItem.Row["Select"]))
                    {
                        view = bindingSource.AddNew() as DataRowView;
                        view.Row["EmployeeId"] = dataBoundItem.Row["EmployeeId"];
                        view.Row["EmpName"] = dataBoundItem.Row["EmpName"];

                        view.Row["DateFrom"] = DateToInfoDateTimePicker.Value;
                        view.Row["DateTo"] = DateFromInfoDateTimePicker.Value;
                        view.Row["VehicleId"] = VehicleIdInfoComboBox.SelectedValue.ToString() ;
                        view.Row["BookTicket"] = BookTicketCheckBox.Checked;
                        view.Row["Causation"] = CausationInfoTextBox.Text;
                        view.Row["CusCompany"] = CusCompanyInfoTextBox.Text;
                        view.Row["CustomerName"] = CustomerNameInfoTextBox.Text;
                        view.Row["IsMulti"] = true ;
                        bindingSource.EndEdit();
                    }
                }
            }
            frm.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            object[] myRet = CliUtils.CallMethod("SWorkTime", "GetWorkOnType", new object[1] { "WorkTime_WorkOnTypeGroup.EmployeeId=" + Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1].ToString()) });

            switch (myRet[1].ToString())
            {
                case "N":
                case "S":
                    oDiff = new object[] { ((DataSet)myRet[2]).Tables[0].Rows[0]["ect"].ToString(), ((DataSet)myRet[2]).Tables[0].Rows[0]["st"].ToString() };
                    break;
                default:
                    oDiff = new object[] { "08:30", "17:30" };
                    break;
            }
        }

        protected object GetDateTo()
        {
            return (oDiff != null ? (Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + oDiff[0].ToString()).AddHours(-1)).ToString() : "");

        }

        protected object GetDateFrom()
        {
            return (oDiff != null ? (Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + oDiff[1].ToString()).AddHours(1)).ToString() : "");
        }
    }
}

